package com.yaozheng.vocationaltraining.view.time;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.DatePicker;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.dialog.interfaces.BaseDialogEventProcess;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.LogUtils;

import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.ViewById;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 选择时间view
 */
@EViewGroup(R.layout.view_dialog_select_time_date_picker)
public class SelectTimeDatePickerDialogView extends RelativeLayout {
    BaseDialogEventProcess baseDialogEventProcess;
    @ViewById
    TextView baseDialogOperateExplanationId;
    @ViewById
    DatePicker baseDialogDatePicker;

    SimpleDateFormat simpleDateFormat;

    public SelectTimeDatePickerDialogView(Context context) {
        super(context);

    }

    public SelectTimeDatePickerDialogView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * 设置数据响应
     *
     * @param baseDialogEventProcess
     */
    public void setBaseDialogEventProcess(BaseDialogEventProcess baseDialogEventProcess) {
        this.baseDialogEventProcess = baseDialogEventProcess;
    }

    public void setTitile(String value) {
        baseDialogOperateExplanationId.setText(value);
    }

    @Click({R.id.baseDialogCancelView, R.id.baseDialogDetermineView})
    public void executeTagClick(View view) {
        if (baseDialogEventProcess != null) {
            baseDialogEventProcess.runMethod(String.valueOf(view.getTag()));
        }
    }

    public SimpleDateFormat getSimpleDateFormat() {
        if (simpleDateFormat == null) {
            simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        }
        return simpleDateFormat;
    }

    /**
     * 设置默认时间
     *
     * @param defaultDate
     */
    public void setDefaultDate(String defaultDate) {
        Calendar currCalendar = Calendar.getInstance();

        try {
            LogUtils.println(defaultDate);
            Date currDate = getSimpleDateFormat().parse(defaultDate);
            currCalendar.setTime(currDate);
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
        }
        baseDialogDatePicker.updateDate(currCalendar.get(Calendar.YEAR), currCalendar.get(Calendar.MONTH), currCalendar.get(Calendar.DAY_OF_MONTH));
    }

    /**
     * 得到当前时间
     *
     * @return
     */
    public String getCurrDate() {
        int y = baseDialogDatePicker.getYear();
        int m = baseDialogDatePicker.getMonth() + 1;
        int d = baseDialogDatePicker.getDayOfMonth();
        return y + "-" + (m < 10 ? "0" + m : m) + "-" + (d < 10 ? "0" + d : d);
    }


    public long getCurrTime() {
        Date currDate = null;
        try {
            currDate = getSimpleDateFormat().parse(getCurrDate());
        } catch (ParseException e) {
            ErrorUtils.outErrorLog(e);
            currDate = new Date();
        }
        return currDate.getTime();
    }
}
